#include "stdafx.h"
#include "ValidatorEach.h"
#include "CombatCard.h"
#include "Utility.h"

void TEachValidators::OnArchieveReaded()
{

	m_vValidators.clear();
	if(m_iID==INVALID_ID) return ;
	if(m_eHpCompare!=eCompare_Null)
	{
		VEach_HpCompare* pV=new VEach_HpCompare();
		pV->InitData((ECompare)m_eHpCompare);
		Add(TValidatorEachPtr(pV));
	}
	if(m_eHpMaxCompare!=eCompare_Null)
	{
		VEach_HpMaxCompare* pV=new VEach_HpMaxCompare();
		pV->InitData((ECompare)m_eHpMaxCompare);
		Add(TValidatorEachPtr(pV));
	}
	if(m_tiBeSameRace!=eTriple_Null)
	{
		VEach_RaceCompare* pV=new VEach_RaceCompare();
		pV->InitData(m_tiBeSameRace==eTriple_True);			
		Add(TValidatorEachPtr(pV));
	}

	if(m_tiBeSelf!=eTriple_Null)
	{
		VEach_SelfCompare* pV=new VEach_SelfCompare();
		pV->InitData(m_tiBeSelf==eTriple_True);			
		Add(TValidatorEachPtr(pV));
	}
	if(!m_vHasBuffOfMine.empty())
	{
		VEach_HasBuffOfMine* pV=new VEach_HasBuffOfMine();
		pV->InitData(m_vHasBuffOfMine);
		Add(TValidatorEachPtr(pV));
	}
	if(!m_vNotHasBuffOfMine.empty())
	{
		VEach_NotHasBuffOfMine* pV=new VEach_NotHasBuffOfMine();
		pV->InitData(m_vNotHasBuffOfMine);
		Add(TValidatorEachPtr(pV));
	}

	if(m_tiBeSameSide!=eTriple_Null)
	{
		VEach_SideCompare* pV=new VEach_SideCompare();
		pV->InitData(m_tiBeSameSide==eTriple_True);			
		Add(TValidatorEachPtr(pV));
	}
}

bool VEach_HpCompare::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	switch(m_eCompare)
	{
	case eCompare_Equal: 
		{
			return (pOwner->GetHp()==pTarget->GetHp());
		}
	case eCompare_Greater:
		{
			return  (pOwner->GetHp()>pTarget->GetHp());
		}
	case eCompare_Less:
		{
			return  (pOwner->GetHp()<pTarget->GetHp());
		}
	case eCompare_Null:
		{
			return true;
		}	
	default: 
		return false;
	}
}

void VEach_HpCompare::InitData( ECompare eCompare )
{
	 m_eCompare=eCompare;
}
bool VEach_RaceCompare::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	if(m_bBeSameRace)
	{
		return pOwner->GetRace()==pTarget->GetRace();
	}
	else
	{
		return pOwner->GetRace()!=pTarget->GetRace();
	}
}

void VEach_RaceCompare::InitData(bool bSameRace )
{
	m_bBeSameRace=bSameRace;
}
bool VEach_SelfCompare::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	if(m_bBeSelfOrNot)
	{
		return pOwner==pTarget;
	}
	else
	{
		return pOwner!=pTarget;
	}
	return true;
}

void VEach_SelfCompare::InitData(bool bSelf)
{
	m_bBeSelfOrNot=bSelf;
}

void VEach_NotHasBuffOfMine::InitData( std::vector<int32>& rData )
{
	m_vReactorIDs=rData;
}

bool VEach_NotHasBuffOfMine::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	if(pTarget->GetObjType()!=ECombatObj_Card) return true;
	CombatCard *pCard=(CombatCard*)pTarget;
	for(uint32 ii=0;ii<m_vReactorIDs.size();ii++)
	{
		if(pCard->GetTypedBuffOfSender(pOwner->GetID(),m_vReactorIDs[ii])) return false;
	}
	return true;
}
void VEach_HasBuffOfMine::InitData( std::vector<int32>& rData )
{
	m_vReactorIDs=rData;
}

bool VEach_HasBuffOfMine::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{

	if(pTarget->GetObjType()!=ECombatObj_Card) return true;
	CombatCard *pCard=(CombatCard*)pTarget;
	for(uint32 ii=0;ii<m_vReactorIDs.size();ii++)
	{
		if(pCard->GetTypedBuffOfSender(pOwner->GetID(),m_vReactorIDs[ii])) return true;
	}
	return false;
}

void VEach_HpMaxCompare::InitData( ECompare eCompare )
{
	m_eCompare=eCompare;
}

bool VEach_HpMaxCompare::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	switch(m_eCompare)
	{
	case eCompare_Equal: 
		{
			return (pOwner->GetHpMax()==pTarget->GetHpMax());
		}
	case eCompare_Greater:
		{
			return  (pOwner->GetHpMax()>pTarget->GetHpMax());
		}
	case eCompare_Less:
		{
			return  (pOwner->GetHpMax()<pTarget->GetHpMax());
		}
	case eCompare_Null:
		{
			return true;
		}	
	default: 
		return false;
	}
}

void VEach_SideCompare::InitData( bool bSameSide )
{
	m_bSameSide=bSameSide;
}

bool VEach_SideCompare::Validate( CombatObj * pOwner,CombatObj * pTarget ) const
{
	if(m_bSameSide)
	{
		return pOwner->GetPlayerID()==pTarget->GetPlayerID();
	}
	else
	{
		return pOwner->GetPlayerID()!=pTarget->GetPlayerID();
	}
}